'use strict';

module.exports = app => {
  app.on('request', ctx => {
    // log receive request
    const { request } = ctx;
    const queryString = ctx.request.querystring
      ? 'Query: ' + ctx.request.querystring
      : '';
    const body = request.rawBody ? 'Body: ' + request.rawBody : '';
    if (ctx.method !== 'HEAD') {
      ctx.logger.info(`---> Init Request ${queryString} ${body}`);
    }
  });
  app.on('response', ctx => {
    const { response } = ctx;
    if (ctx.method !== 'HEAD') {
      if (response.status === 200) {
        ctx.logger.info(
          `<--- HTTP Code: ${response.status} Code: ${
            response.body.code
          } Message: ${response.body.message}`
        );
      }
    }
  });
};
